package org.brisling.oa.service;

import java.util.List;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.oa.domain.OAConferenceroomApply;
import org.brisling.oa.queryService.QueryOAConferenceroomApplyService;
import org.brisling.oa.repository.OAConferenceroomApplyRepo;
import org.brisling.oa.service.base.BaseOaFlowService;
import org.brisling.workflow.domain.VOHistoric;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/oaConferenceroomApply")
/**
 * 用车申请模块service
 * @Author : jackson wang
 * @Date   : 2016年1月25日 下午4:18:01
 * @version: 1.0
 */
public class OAConferenceroomApplyService extends BaseOaFlowService<OAConferenceroomApply>{
	
	//业务模块仓库对象
	@SuppressWarnings("unused")
	@Autowired
	private OAConferenceroomApplyRepo baseFlowRepo;	
	
	//登录用户验证及用户信息服务类
	@Autowired
	private ShiroSecurityService shiroSecurityService;	
	
	@Autowired
	private QueryOAConferenceroomApplyService queryService;
	/**
	 * 构造函数
	 */
	public OAConferenceroomApplyService(){
		//业务实体类
		this.clazz = OAConferenceroomApply.class;
		
		//业务模块定义
		this.moduleName = ConstSysParam.MODULE_CONFERENCEROOM_APPLY;		
	}
	
		
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	/**
	 * 
	 */
	
	@RequestMapping("/flowHistorics.action")
	public List<VOHistoric> getHistoricByProcInstId(@RequestParam(value="flowInstId",required=false) String procInstId){
		return super.getHistoricByProcInstId(procInstId);
	}
	
	/**
	 * 返回缺陷记录流程实例id的执行记录
	 * @param procInstId 流程实例id
	 * @return List<HistoricTaskInstance> 流程实例执行记录
	 */
	@RequestMapping("/flowHistoricsListDomain.action")
	public ListDomain<VOHistoric> getBizHistoricListDomainByProcInstId(@RequestParam(value="flowInstId",required=false) String procInstId){
		return super.getHistoricListDomainByProcInstId(procInstId);
	}
	
	
	
	/**
	 * 删除人员信息
	 * @param id
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		RetDomain domain = new RetDomain();
		if(id!=null){
			if(super.deleteTask(id, null)){
				domain.setSuccessMsg("200");
				return domain;
			}else{
				domain.setErrorMsg("500");
				return domain;
			}
		}else{
			if(ids!=null){
				Integer count =super.deleteTaskList(ids, null);
				domain.setMsg("删除 " + count + " 条记录成功!");
			}
		}
		
		return domain;
		
	}
	
	
	/**
	 * 保存业务数据
	 * @param body	业务数据参数
	 * @return
	 */
	@RequestMapping("/flowSave.action")
	public OAConferenceroomApply save(@RequestBody String body){
		OAConferenceroomApply obj = bodyToObj(body, OAConferenceroomApply.class);	
		if(obj!=null){
			if(obj.getBizno()==null || obj.getBizno().trim().length()<=0){
				obj.setBizno(queryService.generateBizno());
			}
		}
		OAConferenceroomApply domain= this.saveFlowEntity(shiroSecurityService.getCurrentUserid().toString(),obj);
		if((domain!=null) && (domain.getId()!=null)){
			domain = queryService.getOne(domain.getId());
		}
		return domain;
	}
	
	/**
	 * 代码保存业务数据，用于后台自动发起任务
	 * @param userid	当前用户id
	 * @param obj		业务实体对象
	 * @return
	 */
	
	public OAConferenceroomApply autoSave(String userid,OAConferenceroomApply obj){
			
		OAConferenceroomApply recipel= this.saveFlowEntity(userid,obj);
		if((recipel!=null) && (recipel.getId()!=null)){
			recipel = queryService.getOne(recipel.getId());
		}
		return recipel;
	}
	
	/**
	 * 签收业务记录
	 * @param body
	 * @return
	 */
	@RequestMapping("/flowSign.action")
	public ListDomain<Object> saveSingInfo(@RequestBody String body){
		
		
		return this.saveSignInfo(shiroSecurityService.getCurrentUserid().toString(), body);
	}
	
	
	
	
	/**
	 * 节点任务完成方法
	 * @param body
	 * @return
	 */
	@RequestMapping("/flowTaskComplete.action")
	public String completeTask(@RequestBody String body){
		OAConferenceroomApply recipel =this.save(body);
		if(recipel!=null){
			
			return super.completeTaskAndCandidateByEntity(
					shiroSecurityService.getCurrentUserid().toString(),
					recipel, 
					"签收");
		}
		
		return "500";
	}
	/**
	 * 自动发送任务方法
	 * @param userId	任务发送人员id
	 * @param domain	发送的业务单据实体
	 * @return
	 */
	public String completeTask(String userId,OAConferenceroomApply domain){
		domain =this.autoSave(userId,domain);
		
		if(domain!=null && userId!=null){			
			return super.completeTaskAndCandidateByEntity(
					userId,domain,"签收");
		}		
		return "500";		
	}
	
	
	/**
	 * 节点回退方法
	 * @param body
	 * @return
	 */
	@RequestMapping("/flowSendback.action")
	public RetDomain saveSendbackTask(@RequestBody String body){
		OAConferenceroomApply recipel =this.save(body);
		if(recipel!=null){
			return super.saveSendBack(
					shiroSecurityService.getCurrentUserid().toString(), 
					recipel, null, null);
			
		}
		RetDomain domain = new RetDomain();
		domain.setErrorMsg("业务保存失败");
		return domain;
	}
	
	@RequestMapping("/save.action")
	public OAConferenceroomApply saves(@RequestBody String body){
		OAConferenceroomApply role = bodyToObj(body, OAConferenceroomApply.class);		
		super.save(role);
		return role;
	}
}
